<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
          
          <el-col :lg="6" :md="12">
            <el-form-item label="所属届数:">
              <el-select
                clearable
                v-model="where.jieshuo_id"
                placeholder="请选择所属届数"
                class="ele-fluid">
                  <el-option
                  v-for="item in jieshuos"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
                </el-select>
            </el-form-item>
          </el-col>
              
          <el-col :lg="6" :md="12">
            <el-form-item label="直播名称:">
              <el-input
                clearable
                v-model="where.name"
                placeholder="请输入直播名称"/>
            </el-form-item>
          </el-col>
                                
                                          
         <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:live:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:live:dall')">删除
          </el-button>
        </template>
        
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openEdit(row)"
            v-if="permission.includes('sys:live:edit')">修改
          </el-link>
          <el-popconfirm
            class="ele-action"
            title="确定要删除此直播吗？"
            @confirm="remove(row)">
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:live:delete')">删除
            </el-link>
          </el-popconfirm>
        </template>
        
        
         <!-- 一键清空留言板 -->
        <template slot="empty" slot-scope="{row}">
          <el-popconfirm
            class="ele-action"
            title="清空留言板后，留言数据将不能恢复，是否确认清空？"
            @confirm="empty(row)">
            <el-link
              type="warning"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
            >一键清空留言板
            </el-link>
          </el-popconfirm>
        </template>
               
               
           <!-- 直播封面 -->
          <template slot="image" slot-scope="{row}">
            <el-avatar shape="square" :size="50" :src="row.image"/>
          </template>                
                                        
            <!-- 直播推流地址 -->
          <template slot="push" slot-scope="{row}">
              <div>服务器地址：{{ row.rtmp.substr(0, row.rtmp.lastIndexOf('/') + 1)}}</div>
             <div>推流码：{{ row.rtmp.substr(row.rtmp.lastIndexOf('/') + 1)}}</div>
          </template>                               
                                        
                                        
                                        
          <!-- 状态列 -->
          <template slot="status" slot-scope="{row}">
            <el-switch
              v-model="row.status"
              @change="status(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>
                                                
          <!-- 状态列 -->
          <template slot="is_jinyan" slot-scope="{row}">
            <el-switch
              v-model="row.is_jinyan"
              @change="setIsJinyan(row)"
              :active-value="1"
              :inactive-value="2"/>
          </template>
                                                
                                        
                                        
                        </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <live-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import LiveEdit from './live-edit';

export default {
  name: 'ExamLive',
  components: {LiveEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/live/index',
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          columnKey: 'image',
          label: '直播封面',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 100,
          slot: 'image'
        },
              {
          prop: 'name',
          label: '直播名称',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
         {
          prop: 'notes',
          label: '直播人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
                    {
          prop: 'jieshuo_name',
          label: '所属届数',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          columnKey: 'push',
          label: '直播推流地址',
          align: 'center',
          minWidth: 350,
          slot: 'push',
        },
                    {
          prop: 'flv',
          label: '播放地址',
          showOverflowTooltip: false,
          minWidth: 450,
          align: 'center',
        },
                    {
          prop: 'replay',
          label: '录播地址',
          showOverflowTooltip: false,
          minWidth: 400,
          align: 'center',
        },
                          {
          prop: 'status',
          label: '上架状态',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'status',
        },
                                {
          prop: 'is_jinyan',
          label: '是否禁言',
          align: 'center',
          width: 100,
          resizable: false,
          slot: 'is_jinyan',
        },
        
         {
          columnKey: 'empty',
          label: '一键清空留言板',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'empty',
        },
                          {
          prop: 'viewers',
          label: '观看人数',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
                    {
          prop: 'sort',
          label: '显示顺序',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
                    {
          prop: 'live_time',
          label: '直播时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
        },
              {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 150,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      jieshuos: []
    };
  },
  created() {
    this.getJieshuos();
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/live/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 一键清除留言板 */
     empty(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/live/lysDel', {live_id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
        //   this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的直播吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/live/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
                                    
    /* 更改状态 */
    status(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "status": row.status      })
      this.$http.post('/live/status', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.status = !row.status ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
          
    /* 更改状态 */
    setIsJinyan(row) {
      const loading = this.$loading({lock: true});
      let params = Object.assign({
        "id": row.id,
        "is_jinyan": row.is_jinyan      })
      this.$http.post('/live/setIsJinyan', params).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
        } else {
          row.is_jinyan = !row.is_jinyan ? 1 : 2;
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    
    splitField(rtmp) {
      // 实现具体的分割逻辑
      let protocol = rtmp.substr(0, rtmp.lastIndexOf('/') + 1);
      let streamInfo = rtmp.substr(rtmp.lastIndexOf('/') + 1);
      return `<p>服务器地址：` + protocol + `</p><p>` + '推流码：' + streamInfo + `</p>`;
    },
    
    getJieshuos(){
      this.$http
        .post("/jieshuo/index", {})
        .then(res => {
          if (res.data.code === 0) {
            this.jieshuos = res.data.data;
           
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(e => {
          this.$message.error(e.message);
        });
    }
        
  }
}
</script>

<style scoped>
</style>
